﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fis.Service.Rules
{
    public class PersonViewProvider : Agile.Engine.View.ProxyViewProvider
    {
        public override System.Collections.Hashtable InitializeViewModel(System.Collections.Hashtable args)
        {
            //addNew
            if (args == null)
            {
                args = new System.Collections.Hashtable();
                args["action"] = "schema";
            }
            else //edit
            {
                args["action"] = "load";
                args["SID"] = args["SID"];
            }

            if (InvokeService(args))
            {
                var data = Agile.Engine.Utils.ZipUtils.UnZipDataTable(ServiceProcess.Result["data"].ToString());
                args["data"] = data;
            }

            return args;
        }

        public override bool ExecuteUpdate(System.Collections.Hashtable viewModel)
        {
            viewModel["state"] = (int)ViewState;
            viewModel["data"] = Agile.Engine.Utils.ZipUtils.ZipDataTable(viewModel["data"] as System.Data.DataTable);
            var exec = InvokeService(viewModel);
            if (exec)
            {
                viewModel["data"] = Agile.Engine.Utils.ZipUtils.UnZipDataTable(ServiceProcess.Result["data"].ToString());
                if (ServiceProcess.Result.ContainsKey("Random"))
                {
                    viewModel["Ramdom"] = ServiceProcess.Result["Random"];
                }
                View.ViewModel = viewModel;
            }

            View.Message = ServiceProcess.Message;
            return exec;
        }

    }
}
